package com.orhon.smartcampus.modules.core.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.orhon.smartcampus.modules.systemctl.entity.OperationApi;
import com.orhon.smartcampus.modules.systemctl.entity.OperationUserRelation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AuthData {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    AuthDao authDao;



    @Data
    class TempObj{
        public Integer id;
        public String name;
    }
    @Data
    class TempApi{
        public Object id;
    }


    public void op(){
        JSONObject obj = new JSONObject();
        List<OperationUserRelation> users = authDao.findOperationList1(null);
        for (OperationUserRelation user : users) {
            JSONArray ap = new JSONArray();
            JSONArray ap1 = new JSONArray();
            JSONArray ap2 = new JSONArray();
            TempApi tem = new TempApi();
            tem.setId(user.getUserUsersByUserId().getId());
            ap.add(tem);
            List<OperationUserRelation> operIds = authDao.findOperationList1(user.getUserUsersByUserId().getId());
            for (OperationUserRelation oper : operIds) {
                TempApi tem1 = new TempApi();
                tem1.setId(oper.getSystemctlOperationsByOperationId().getId());
                ap1.add(tem1);

                if(!"".equals( oper.getSystemctlOperationsByOperationId().getId())){
                    List<OperationApi> apis = authDao.findApi1(oper.getSystemctlOperationsByOperationId().getId());//三次
                    int e=0;
                    for (int i=0;i<apis.size();i++) {
                        e++;
                        TempObj api =new TempObj();
                        api.setName(apis.get(i).getApi_code());
                        ap2.add(api);
                    }
                    obj.put("apis",ap2);
                }
            }
            obj.put("oprations",ap1);
            obj.put("user",ap);
            stringRedisTemplate.opsForValue().set("user_"+user.getUserUsersByUserId().getId(), String.valueOf(obj));
        }
    }
}
